home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- ** This program creates two GLX contexts, and alternately draws into the
- ** same window with them.
- */
-
- #define NeedFunctionPrototypes 1
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/keysym.h>
- #include <GL/gl.h>
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /*
- ** Convenience routines for creating and configuring windows.
- **
- ** $Revision: 1.3 $
- ** $Date: 1993/08/09 22:39:51 $
- */
-
- Window SetupWindow(Display *dpy, int width, int height, XVisualInfo *vi,
- unsigned long background)
- {
- int scr;
- unsigned long mask;
- Visual *vis = vi->visual;
- Colormap cmap;
- Window win, parent;
- XSetWindowAttributes wa;
-
- scr = DefaultScreen(dpy);
- parent = RootWindow(dpy, scr);
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, scr), vis, AllocNone);
-
- wa.colormap = cmap;
- wa.background_pixel = background;
- wa.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
- mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
-
- /*
- ** Create window
- */
- win = XCreateWindow(dpy, parent, 0, 0, width, height, 0, vi->depth,
- InputOutput, vis, mask, &wa);
- XMapWindow(dpy, win);
- return win;
- }
-
- #define WIDTH 200
- #define HEIGHT 200
-
- int screen;
- Window win;
- Display *dpy;
- XVisualInfo *vis;
-
- GLXContext leftContext, rightContext;
-
- void init(void)
- {
- glEnable(GL_SCISSOR_TEST);
- glEnable(GL_DEPTH_TEST);
-
- glViewport(10, 10, WIDTH-20, HEIGHT-20);
- glScissor(10, 10, WIDTH-20, HEIGHT-20);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
- glMatrixMode(GL_MODELVIEW);
-
- glClearColor(0.0, 0.0, 0.0, 0.0);
-
- glDisable(GL_POLYGON_SMOOTH);
- glDisable(GL_BLEND);
- }
-
- void redraw(void)
- {
- if (!glXMakeCurrent(dpy, win, rightContext)) {
- printf("glXMakeCurrent on right context failed\n");
- }
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBegin(GL_TRIANGLES);
- glVertex3f( 0.9, -0.9, -30.0);
- glVertex3f( 0.9, 0.9, -30.0);
- glVertex3f(-0.9, 0.0, -30.0);
- glEnd();
- glFlush();
-
- if (!glXMakeCurrent(dpy, win, leftContext)) {
- printf("glXMakeCurrent on left context failed\n");
- }
- glBegin(GL_TRIANGLES);
- glVertex3f(-0.9, -0.9, -40.0);
- glVertex3f(-0.9, 0.9, -40.0);
- glVertex3f( 0.9, 0.0, -25.0);
- glEnd();
- glFlush();
- }
-
-
- float colors[][3] = {
- {1, 1, 0},
- {0, 1, 1},
- {1, 0, 1},
- };
-
- void copy(void)
- {
- GLXContext thirdContext;
- int ncolors = sizeof(colors) / sizeof(colors[0]);
- static int index=0;
-
- /*
- ** Make a red context.
- */
- thirdContext = glXCreateContext(dpy, vis, None, GL_TRUE);
- if (!glXMakeCurrent(dpy, win, thirdContext)) {
- printf("glXMakeCurrent on third context failed\n");
- }
- init();
- glColor3fv(colors[index++ % ncolors]);
- glFlush();
- glXMakeCurrent(dpy, None, None);
- /*
- ** Copy attributes of the third context to the left context.
- */
- glXCopyContext(dpy, thirdContext, leftContext, GL_CURRENT_BIT);
- glXDestroyContext(dpy, thirdContext);
- redraw();
- }
-
- int attribList[] = {
- GLX_RGBA, GL_TRUE,
- GLX_DEPTH_SIZE, 1,
- None,
- };
-
- void main(int argc, char **argv)
- {
- char buf[100];
- XEvent event;
- KeySym ks;
- unsigned long mask;
- Colormap cmap;
- Window parent;
- XSetWindowAttributes wa;
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display\n");
- return;
- }
- screen = DefaultScreen(dpy);
- parent = RootWindow(dpy, screen);
- vis = glXChooseVisual(dpy, screen, attribList);
- if (!vis) {
- fprintf(stderr, "Can't find suitable visual\n");
- return;
- }
-
- win = SetupWindow(dpy, WIDTH, HEIGHT, vis, BlackPixel(dpy,screen));
-
- leftContext = glXCreateContext(dpy, vis, None, GL_TRUE);
- rightContext = glXCreateContext(dpy, vis, None, GL_TRUE);
-
- if (!glXMakeCurrent(dpy, win, leftContext)) {
- printf("glXMakeCurrent on left context failed\n");
- }
- glColor3f(0, 1, 0);
- init();
-
- if (!glXMakeCurrent(dpy, win, rightContext)) {
- printf("glXMakeCurrent on right context failed\n");
- }
- glColor3f(0, 0, 1);
- init();
-
- /*
- ** Redraw when you hit 'd'.
- ** Copy context when you hit 'c'.
- */
- while (1) {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- if (!glXMakeCurrent(dpy, win, leftContext)) {
- printf("glXMakeCurrent on left context failed\n");
- }
- init();
-
- if (!glXMakeCurrent(dpy, win, rightContext)) {
- printf("glXMakeCurrent on right context failed\n");
- }
- init();
- redraw();
- break;
- case KeyPress:
- XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_c:
- case XK_C:
- copy();
- break;
- case XK_d:
- case XK_D:
- redraw();
- break;
- case XK_Escape:
- goto done;
- }
- }
- }
- done:
- glXMakeCurrent(dpy, None, NULL);
- glXDestroyContext(dpy, leftContext);
- glXDestroyContext(dpy, rightContext);
- XCloseDisplay(dpy);
- }
-